package com.dormitory.management.system.config;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.dormitory.management.system.utils.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Configuration
public class AlipayConfig {
    //appid
    @Value("${alipay.appId}")
    private String appId;
    //协议
    @Value("${alipay.protocol}")
    private String protocol;
    //网关
    @Value("${alipay.gatewayHost}")
    private String gatewayHost;
    //RSA2
    @Value("${alipay.signType}")
    private String signType;
    //私钥
    @Value("${alipay.merchantPrivateKey}")
    private String merchantPrivateKey;
    //应用公钥证书
    @Value("${alipay.merchantCertPath}")
    private String merchantCertPath;
    //支付宝公钥证书
    @Value("${alipay.alipayCertPath}")
    private String alipayCertPath;
    //支付宝根证书
    @Value("${alipay.alipayRootCertPath}")
    private String alipayRootCertPath;
    //可设置异步通知接收服务地址
    @Value("${alipay.notifyUrl}")
    private String notifyUrl;

    @Bean
    public Config getAlipayConfig() throws IOException {
        Config config=new Config();
        config.appId=appId;
        config.protocol=protocol;
        config.gatewayHost=gatewayHost;
        config.signType=signType;
        config.merchantPrivateKey = FileUtils.readFileOfTxt(new ClassPathResource(new String(merchantPrivateKey.getBytes(), StandardCharsets.UTF_8)).getInputStream());
        config.merchantCertPath = new ClassPathResource(merchantCertPath).getPath();
        config.alipayCertPath = new ClassPathResource(alipayCertPath).getPath();
        config.alipayRootCertPath = new ClassPathResource(alipayRootCertPath).getPath();
        config.notifyUrl=notifyUrl;
        //只需全局设置一次
        Factory.setOptions(config);
        return config;
    }
}


